<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">账号：</label>
                        <div class="layui-input-inline mr0">
                            <input name="account" class="layui-input" type="text" placeholder="请输入账号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">日期：</label>
                        <div class="layui-input-inline mr0">
                            <input name="dateRange" class="layui-input date-icon" type="text" placeholder="请选择日期范围"
                                   autocomplete="off"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnExpLog" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableLog" lay-filter="tableLog"></table>
        </div>
    </div>
</div>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var laydate = layui.laydate;
        var config = layui.config;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableLog',
            url: config.base_server + 'loginRecord.json',
            page: true,
            title: '登录日志',
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {field: 'username', sort: true, title: '账号'},
                {field: 'nickName', sort: true, title: '用户名'},
                {field: 'ipAddress', sort: true, title: 'IP'},
                {field: 'device', sort: true, title: '设备'},
                {field: 'osName', sort: true, title: '设备类型'},
                {field: 'browserType', sort: true, title: '浏览器'},
                {
                    field: 'createTime', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '登录时间'
                }
            ]]
        });

        // 时间范围
        laydate.render({
            elem: 'input[name="dateRange"]',
            type: 'date',
            range: true
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            if (data.field.dateRange) {
                var searchDate = data.field.dateRange.split(' - ');
                data.field.startDate = searchDate[0];
                data.field.endDate = searchDate[1];
            }
            data.field.dateRange = undefined;
            insTb.reload({where: data.field}, 'data');
        });

        // 导出excel
        $('#btnExpLog').click(function () {
            var checkRows = table.checkStatus('tableLog');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>